home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / Firefox Setup 3.0.6.exe / nonlocalized / components / nsSafebrowsingApplication.js < prev    next >
Encoding:
Text File  |  2009-01-19  |  24.6 KB  |  738 lines

  1. const Cc = Components.classes;
  2. const Ci = Components.interfaces;
  3.  
  4. // This is copied from toolkit/components/content/js/lang.js.
  5. // It seems cleaner to copy this rather than #include from so far away.
  6. Function.prototype.inherits = function(parentCtor) {
  7.   var tempCtor = function(){};
  8.   tempCtor.prototype = parentCtor.prototype;
  9.   this.superClass_ = parentCtor.prototype;
  10.   this.prototype = new tempCtor();
  11. }  
  12.  
  13. //@line 36 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\application.js"
  14.  
  15. // We instantiate this variable when we create the application.
  16. var gDataProvider = null;
  17.  
  18. // An instance of our application is a PROT_Application object. It
  19. // basically just populates a few globals and instantiates wardens,
  20. // the listmanager, and the about:blocked error page.
  21.  
  22. /**
  23.  * An instance of our application. There should be exactly one of these.
  24.  * 
  25.  * Note: This object should instantiated only at profile-after-change
  26.  * or later because the listmanager and the cryptokeymanager need to
  27.  * read and write data files. Additionally, NSS isn't loaded until
  28.  * some time around then (Moz bug #321024).
  29.  *
  30.  * @constructor
  31.  */
  32. function PROT_Application() {
  33.   this.debugZone= "application";
  34.  
  35. //@line 83 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\application.js"
  36.   
  37.   // expose some classes
  38.   this.PROT_PhishingWarden = PROT_PhishingWarden;
  39.   this.PROT_MalwareWarden = PROT_MalwareWarden;
  40.  
  41.   // Load data provider pref values
  42.   gDataProvider = new PROT_DataProvider();
  43.  
  44.   // expose the object
  45.   this.wrappedJSObject = this;
  46. }
  47.  
  48. var gInitialized = false;
  49. PROT_Application.prototype.initialize = function() {
  50.   if (gInitialized)
  51.     return;
  52.   gInitialized = true;
  53.  
  54.   var obs = Cc["@mozilla.org/observer-service;1"].
  55.             getService(Ci.nsIObserverService);
  56.   obs.addObserver(this, "xpcom-shutdown", true);
  57.  
  58.   // XXX: move table names to a pref that we originally will download
  59.   // from the provider (need to workout protocol details)
  60.   this.malwareWarden = new PROT_MalwareWarden();
  61.   this.malwareWarden.registerBlackTable("goog-malware-shavar");
  62.   this.malwareWarden.maybeToggleUpdateChecking();
  63.  
  64.   this.phishWarden = new PROT_PhishingWarden();
  65.   this.phishWarden.registerBlackTable("goog-phish-shavar");
  66.   this.phishWarden.maybeToggleUpdateChecking();
  67. }
  68.  
  69. PROT_Application.prototype.observe = function(subject, topic, data) {
  70.   switch (topic) {
  71.     case "xpcom-shutdown":
  72.       this.malwareWarden.shutdown();
  73.       this.phishWarden.shutdown();
  74.       break;
  75.   }
  76. }
  77.  
  78. /**
  79.  * @param name String The type of url to get (either Phish or Error).
  80.  * @return String the report phishing URL (localized).
  81.  */
  82. PROT_Application.prototype.getReportURL = function(name) {
  83.   return gDataProvider["getReport" + name + "URL"]();
  84. }
  85.  
  86. /**
  87.  * about:blocked implementation
  88.  */
  89. PROT_Application.prototype.newChannel = function(uri) {
  90.   var ioService = Cc["@mozilla.org/network/io-service;1"]
  91.                  .getService(Ci.nsIIOService);
  92.   var secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
  93.               .getService(Ci.nsIScriptSecurityManager);
  94.  
  95.   var childURI = ioService.newURI("chrome://browser/content/safebrowsing/blockedSite.xhtml",
  96.                                   null, null);
  97.   var channel = ioService.newChannelFromURI(childURI);
  98.   channel.originalURI = uri;
  99.   
  100.   // Drop chrome privilege
  101.   var principal = secMan.getCodebasePrincipal(uri);
  102.   channel.owner = principal;
  103.  
  104.   return channel;
  105. }
  106.  
  107. PROT_Application.prototype.getURIFlags = function(uri) {
  108.   // We don't particularly *want* people linking to this from
  109.   // untrusted content, but given that bad sites can cause this page
  110.   // to appear (e.g. by having an iframe pointing to known malware),
  111.   // we should code as though this is explicitly possible.
  112.   return Ci.nsIAboutModule.ALLOW_SCRIPT |
  113.          Ci.nsIAboutModule.URI_SAFE_FOR_UNTRUSTED_CONTENT;
  114. }
  115.  
  116. PROT_Application.prototype.QueryInterface = function(iid) {
  117.   if (iid.equals(Ci.nsISupports) ||
  118.       iid.equals(Ci.nsISupportsWeakReference) ||
  119.       iid.equals(Ci.nsIObserver) ||
  120.       iid.equals(Ci.nsIAboutModule))
  121.     return this;
  122.  
  123.   Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  124.   return null;
  125. }
  126. //@line 37 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\globalstore.js"
  127.  
  128.  
  129. // A class that encapsulates data provider specific values.  The
  130. // root of the provider pref tree is browser.safebrowsing.provider.
  131. // followed by a number, followed by specific properties.  The properties
  132. // that a data provider can supply are:
  133. //
  134. // name: The name of the provider
  135. // lookupURL: The URL to send requests to in enhanced mode
  136. // keyURL: Before we send URLs in enhanced mode, we need to encrypt them
  137. // reportURL: When shown a warning bubble, we send back the user decision
  138. //            (get me out of here/ignore warning) to this URL (strip cookies
  139. //            first).  This is optional.
  140. // gethashURL: Url for requesting complete hashes from the provider.
  141. // reportGenericURL: HTML page for general user feedback
  142. // reportPhishURL: HTML page for notifying the provider of a new phishing page
  143. // reportErrorURL: HTML page for notifying the provider of a false positive
  144.  
  145. const kDataProviderIdPref = 'browser.safebrowsing.dataProvider';
  146. const kProviderBasePref = 'browser.safebrowsing.provider.';
  147.  
  148. //@line 59 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\globalstore.js"
  149. const MOZ_OFFICIAL_BUILD = true;
  150. //@line 63 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\globalstore.js"
  151.  
  152. const MOZ_PARAM_LOCALE = /\{moz:locale\}/g;
  153. const MOZ_PARAM_CLIENT = /\{moz:client\}/g;
  154. const MOZ_PARAM_BUILDID = /\{moz:buildid\}/g;
  155. const MOZ_PARAM_VERSION = /\{moz:version\}/g;
  156.  
  157. /**
  158.  * Information regarding the data provider.
  159.  */
  160. function PROT_DataProvider() {
  161.   this.prefs_ = new G_Preferences();
  162.  
  163.   this.loadDataProviderPrefs_();
  164.   
  165.   // Watch for changes in the data provider and update accordingly.
  166.   this.prefs_.addObserver(kDataProviderIdPref,
  167.                           BindToObject(this.loadDataProviderPrefs_, this));
  168.  
  169.   // Watch for when anti-phishing is toggled on or off.
  170.   this.prefs_.addObserver(kPhishWardenEnabledPref,
  171.                           BindToObject(this.loadDataProviderPrefs_, this));
  172. }
  173.  
  174. /**
  175.  * Populate all the provider variables.  We also call this when whenever
  176.  * the provider id changes.
  177.  */
  178. PROT_DataProvider.prototype.loadDataProviderPrefs_ = function() {
  179.   // Currently, there's no UI for changing local list provider so we
  180.   // hard code the value for provider 0.
  181.   this.updateURL_ = this.getUrlPref_(
  182.         'browser.safebrowsing.provider.0.updateURL');
  183.  
  184.   var id = this.prefs_.getPref(kDataProviderIdPref, null);
  185.  
  186.   // default to 0
  187.   if (null == id)
  188.     id = 0;
  189.   
  190.   var basePref = kProviderBasePref + id + '.';
  191.  
  192.   this.name_ = this.prefs_.getPref(basePref + "name", "");
  193.  
  194.   // Urls used to get data from a provider
  195.   this.lookupURL_ = this.getUrlPref_(basePref + "lookupURL");
  196.   this.keyURL_ = this.getUrlPref_(basePref + "keyURL");
  197.   this.reportURL_ = this.getUrlPref_(basePref + "reportURL");
  198.   this.gethashURL_ = this.getUrlPref_(basePref + "gethashURL");
  199.  
  200.   // Urls to HTML report pages
  201.   this.reportGenericURL_ = this.getUrlPref_(basePref + "reportGenericURL");
  202.   this.reportErrorURL_ = this.getUrlPref_(basePref + "reportErrorURL");
  203.   this.reportPhishURL_ = this.getUrlPref_(basePref + "reportPhishURL");
  204.  
  205.   // Propagate the changes to the list-manager.
  206.   this.updateListManager_();
  207. }
  208.  
  209. /**
  210.  * The list manager needs urls to operate.  It needs a url to know where the
  211.  * table updates are, and it needs a url for decrypting enchash style tables.
  212.  */
  213. PROT_DataProvider.prototype.updateListManager_ = function() {
  214.   var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
  215.                       .getService(Ci.nsIUrlListManager);
  216.  
  217.   // If we add support for changing local data providers, we need to add a
  218.   // pref observer that sets the update url accordingly.
  219.   listManager.setUpdateUrl(this.getUpdateURL());
  220.  
  221.   // setKeyUrl has the side effect of fetching a key from the server.
  222.   // This shouldn't happen if anti-phishing/anti-malware is disabled.
  223.   var isEnabled = this.prefs_.getPref(kPhishWardenEnabledPref, false) ||
  224.                   this.prefs_.getPref(kMalwareWardenEnabledPref, false);
  225.   if (isEnabled) {
  226.     listManager.setKeyUrl(this.keyURL_);
  227.   }
  228.  
  229.   listManager.setGethashUrl(this.getGethashURL());
  230. }
  231.  
  232. /**
  233.  * Lookup the value of a URL from prefs file and do parameter substitution.
  234.  */
  235. PROT_DataProvider.prototype.getUrlPref_ = function(prefName) {
  236.   var url = this.prefs_.getPref(prefName);
  237.  
  238.   var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
  239.                           .getService(Components.interfaces.nsIXULAppInfo);
  240.  
  241.   var mozClientStr = this.prefs_.getPref("browser.safebrowsing.clientid",
  242.                                          MOZ_OFFICIAL_BUILD ? 'navclient-auto-ffox' : appInfo.name);
  243.  
  244.   var versionStr = this.prefs_.getPref("browser.safebrowsing.clientver",
  245.                                        appInfo.version);
  246.  
  247.   // Parameter substitution
  248.   url = url.replace(MOZ_PARAM_LOCALE, this.getLocale_());
  249.   url = url.replace(MOZ_PARAM_CLIENT, mozClientStr);
  250.   url = url.replace(MOZ_PARAM_BUILDID, appInfo.appBuildID);
  251.   url = url.replace(MOZ_PARAM_VERSION, versionStr);
  252.   return url;
  253. }
  254.  
  255. /**
  256.  * @return String the browser locale (similar code is in nsSearchService.js)
  257.  */
  258. PROT_DataProvider.prototype.getLocale_ = function() {
  259.   const localePref = "general.useragent.locale";
  260.   var locale = this.getLocalizedPref_(localePref);
  261.   if (locale)
  262.     return locale;
  263.  
  264.   // Not localized
  265.   var prefs = new G_Preferences();
  266.   return prefs.getPref(localePref, "");
  267. }
  268.  
  269. /**
  270.  * @return String name of the localized pref, null if none exists.
  271.  */
  272. PROT_DataProvider.prototype.getLocalizedPref_ = function(aPrefName) {
  273.   // G_Preferences doesn't know about complex values, so we use the
  274.   // xpcom object directly.
  275.   var prefs = Cc["@mozilla.org/preferences-service;1"]
  276.               .getService(Ci.nsIPrefBranch);
  277.   try {
  278.     return prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data;
  279.   } catch (ex) {
  280.   }
  281.   return "";
  282. }
  283.  
  284. //////////////////////////////////////////////////////////////////////////////
  285. // Getters for the remote provider pref values mentioned above.
  286. PROT_DataProvider.prototype.getName = function() {
  287.   return this.name_;
  288. }
  289.  
  290. PROT_DataProvider.prototype.getUpdateURL = function() {
  291.   return this.updateURL_;
  292. }
  293.  
  294. PROT_DataProvider.prototype.getLookupURL = function() {
  295.   return this.lookupURL_;
  296. }
  297.  
  298. PROT_DataProvider.prototype.getGethashURL = function() {
  299.   return this.gethashURL_;
  300. }
  301.  
  302. PROT_DataProvider.prototype.getReportGenericURL = function() {
  303.   return this.reportGenericURL_;
  304. }
  305. PROT_DataProvider.prototype.getReportErrorURL = function() {
  306.   return this.reportErrorURL_;
  307. }
  308. PROT_DataProvider.prototype.getReportPhishURL = function() {
  309.   return this.reportPhishURL_;
  310. }
  311. //@line 37 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\list-warden.js"
  312.  
  313. // A warden that knows how to register lists with a listmanager and keep them
  314. // updated if necessary.  The ListWarden also provides a simple interface to
  315. // check if a URL is evil or not.  Specialized wardens like the PhishingWarden
  316. // inherit from it.
  317. //
  318. // Classes that inherit from ListWarden are responsible for calling
  319. // enableTableUpdates or disableTableUpdates.  This usually entails
  320. // registering prefObservers and calling enable or disable in the base
  321. // class as appropriate.
  322. //
  323.  
  324. /**
  325.  * Abtracts the checking of user/browser actions for signs of
  326.  * phishing. 
  327.  *
  328.  * @constructor
  329.  */
  330. function PROT_ListWarden() {
  331.   this.debugZone = "listwarden";
  332.   var listManager = Cc["@mozilla.org/url-classifier/listmanager;1"]
  333.                       .getService(Ci.nsIUrlListManager);
  334.   this.listManager_ = listManager;
  335.  
  336.   // Once we register tables, their respective names will be listed here.
  337.   this.blackTables_ = [];
  338.   this.whiteTables_ = [];
  339. }
  340.  
  341. PROT_ListWarden.IN_BLACKLIST = 0
  342. PROT_ListWarden.IN_WHITELIST = 1
  343. PROT_ListWarden.NOT_FOUND = 2
  344.  
  345. /**
  346.  * Tell the ListManger to keep all of our tables updated
  347.  */
  348.  
  349. PROT_ListWarden.prototype.enableBlacklistTableUpdates = function() {
  350.   for (var i = 0; i < this.blackTables_.length; ++i) {
  351.     this.listManager_.enableUpdate(this.blackTables_[i]);
  352.   }
  353. }
  354.  
  355. /**
  356.  * Tell the ListManager to stop updating our tables
  357.  */
  358.  
  359. PROT_ListWarden.prototype.disableBlacklistTableUpdates = function() {
  360.   for (var i = 0; i < this.blackTables_.length; ++i) {
  361.     this.listManager_.disableUpdate(this.blackTables_[i]);
  362.   }
  363. }
  364.  
  365. /**
  366.  * Tell the ListManager to update whitelist tables.  They may be enabled even
  367.  * when other updates aren't, for performance reasons.
  368.  */
  369. PROT_ListWarden.prototype.enableWhitelistTableUpdates = function() {
  370.   for (var i = 0; i < this.whiteTables_.length; ++i) {
  371.     this.listManager_.enableUpdate(this.whiteTables_[i]);
  372.   }
  373. }
  374.  
  375. /**
  376.  * Tell the ListManager to stop updating whitelist tables.
  377.  */
  378. PROT_ListWarden.prototype.disableWhitelistTableUpdates = function() {
  379.   for (var i = 0; i < this.whiteTables_.length; ++i) {
  380.     this.listManager_.disableUpdate(this.whiteTables_[i]);
  381.   }
  382. }
  383.  
  384. /**
  385.  * Register a new black list table with the list manager
  386.  * @param tableName - name of the table to register
  387.  * @returns true if the table could be registered, false otherwise
  388.  */
  389.  
  390. PROT_ListWarden.prototype.registerBlackTable = function(tableName) {
  391.   var result = this.listManager_.registerTable(tableName, false);
  392.   if (result) {
  393.     this.blackTables_.push(tableName);
  394.   }
  395.   return result;
  396. }
  397.  
  398. /**
  399.  * Register a new white list table with the list manager
  400.  * @param tableName - name of the table to register
  401.  * @returns true if the table could be registered, false otherwise
  402.  */
  403.  
  404. PROT_ListWarden.prototype.registerWhiteTable = function(tableName) {
  405.   var result = this.listManager_.registerTable(tableName, false);
  406.   if (result) {
  407.     this.whiteTables_.push(tableName);
  408.   }
  409.   return result;
  410. }
  411. //@line 36 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\phishing-warden.js"
  412.  
  413.  
  414. // The warden checks request to see if they are for phishy pages. It
  415. // does so by querying our locally stored blacklists.
  416. // 
  417. // When the warden notices a problem, it queries all browser views
  418. // (each of which corresopnds to an open browser window) to see
  419. // whether one of them can handle it. A browser view can handle a
  420. // problem if its browser window has an HTMLDocument loaded with the
  421. // given URL and that Document hasn't already been flagged as a
  422. // problem. For every problematic URL we notice loading, at most one
  423. // Document is flagged as problematic. Otherwise you can get into
  424. // trouble if multiple concurrent phishy pages load with the same URL.
  425. //
  426. // Since we check URLs very early in the request cycle (in a progress
  427. // listener), the URL might not yet be associated with a Document when
  428. // we determine that it is phishy. So the the warden retries finding
  429. // a browser view to handle the problem until one can, or until it
  430. // determines it should give up (see complicated logic below).
  431. //
  432. // The warden has displayers that the browser view uses to render
  433. // different kinds of warnings (e.g., one that's shown before a page
  434. // loads as opposed to one that's shown after the page has already
  435. // loaded).
  436. //
  437. // Note: There is a single warden for the whole application.
  438. //
  439. // TODO better way to expose displayers/views to browser view
  440.  
  441. const kPhishWardenEnabledPref = "browser.safebrowsing.enabled";
  442.  
  443. /**
  444.  * Abtracts the checking of user/browser actions for signs of
  445.  * phishing. 
  446.  *
  447.  * @param progressListener nsIDocNavStartProgressListener
  448.  * @param tabbrowser XUL tabbrowser element
  449.  * @constructor
  450.  */
  451. function PROT_PhishingWarden() {
  452.   PROT_ListWarden.call(this);
  453.  
  454.   this.debugZone = "phishwarden";
  455.  
  456.   // Use this to query preferences
  457.   this.prefs_ = new G_Preferences();
  458.  
  459.   // We need to know whether we're enabled and whether we're in advanced
  460.   // mode, so reflect the appropriate preferences into our state.
  461.  
  462.   // Global preference to enable the phishing warden
  463.   this.phishWardenEnabled_ = this.prefs_.getPref(kPhishWardenEnabledPref, null);
  464.  
  465.   // Get notifications when the phishing warden enabled pref changes
  466.   var phishWardenPrefObserver = 
  467.     BindToObject(this.onPhishWardenEnabledPrefChanged, this);
  468.   this.prefs_.addObserver(kPhishWardenEnabledPref, phishWardenPrefObserver);
  469.  
  470.   G_Debug(this, "phishWarden initialized");
  471. }
  472.  
  473. PROT_PhishingWarden.inherits(PROT_ListWarden);
  474.  
  475. PROT_PhishingWarden.prototype.QueryInterface = function(iid) {
  476.   if (iid.equals(Ci.nsISupports) || 
  477.       iid.equals(Ci.nsISupportsWeakReference))
  478.     return this;
  479.   throw Components.results.NS_ERROR_NO_INTERFACE;
  480. }
  481.  
  482. /**
  483.  * Cleanup on shutdown.
  484.  */
  485. PROT_PhishingWarden.prototype.shutdown = function() {
  486.   this.prefs_.removeAllObservers();
  487.   this.listManager_ = null;
  488. }
  489.  
  490. /**
  491.  * When a preference (either advanced features or the phishwarden
  492.  * enabled) changes, we might have to start or stop asking for updates. 
  493.  * 
  494.  * This is a little tricky; we start or stop management only when we
  495.  * have complete information we can use to determine whether we
  496.  * should.  It could be the case that one pref or the other isn't set
  497.  * yet (e.g., they haven't opted in/out of advanced features). So do
  498.  * nothing unless we have both pref values -- we get notifications for
  499.  * both, so eventually we will start correctly.
  500.  */ 
  501. PROT_PhishingWarden.prototype.maybeToggleUpdateChecking = function() {
  502.   var phishWardenEnabled = this.prefs_.getPref(kPhishWardenEnabledPref, null);
  503.  
  504.   G_Debug(this, "Maybe toggling update checking. " +
  505.           "Warden enabled? " + phishWardenEnabled);
  506.  
  507.   // Do nothing unless both prefs are set.  They can be null (unset), true, or
  508.   // false.
  509.   if (phishWardenEnabled === null)
  510.     return;
  511.  
  512.   // We update and save to disk all tables
  513.   if (phishWardenEnabled === true) {
  514.     this.enableBlacklistTableUpdates();
  515.     this.enableWhitelistTableUpdates();
  516.   } else {
  517.     // Anti-phishing is off, disable table updates
  518.     this.disableBlacklistTableUpdates();
  519.     this.disableWhitelistTableUpdates();
  520.   }
  521. }
  522.  
  523. /**
  524.  * Deal with a user changing the pref that says whether we should 
  525.  * enable the phishing warden (i.e., that SafeBrowsing is active)
  526.  *
  527.  * @param prefName Name of the pref holding the value indicating whether
  528.  *                 we should enable the phishing warden
  529.  */
  530. PROT_PhishingWarden.prototype.onPhishWardenEnabledPrefChanged = function(
  531.                                                                     prefName) {
  532.   // Just to be safe, ignore changes to sub prefs.
  533.   if (prefName != "browser.safebrowsing.enabled")
  534.     return;
  535.  
  536.   this.phishWardenEnabled_ = 
  537.     this.prefs_.getPref(prefName, this.phishWardenEnabled_);
  538.   this.maybeToggleUpdateChecking();
  539. }
  540. //@line 37 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\content\malware-warden.js"
  541.  
  542. // This warden manages updates to the malware list
  543.  
  544. const kMalwareWardenEnabledPref = "browser.safebrowsing.malware.enabled";
  545.  
  546. function PROT_MalwareWarden() {
  547.   PROT_ListWarden.call(this);
  548.  
  549.   this.debugZone = "malwarewarden";
  550.  
  551.   // Use this to query preferences
  552.   this.prefs_ = new G_Preferences();
  553.  
  554.   // Global preference to enable the malware warden
  555.   this.malwareWardenEnabled_ =
  556.     this.prefs_.getPref(kMalwareWardenEnabledPref, null);
  557.  
  558.   // Get notifications when the malware warden enabled pref changes
  559.   var malwareWardenPrefObserver =
  560.     BindToObject(this.onMalwareWardenEnabledPrefChanged, this);
  561.   this.prefs_.addObserver(kMalwareWardenEnabledPref, malwareWardenPrefObserver);
  562.  
  563.   // Add a test chunk to the database
  564.   var testData = "mozilla.com/firefox/its-an-attack.html";
  565.  
  566.   var testUpdate =
  567.     "n:1000\ni:test-malware-simple\nad:1\n" +
  568.     "a:1:32:" + testData.length + "\n" +
  569.     testData;
  570.     
  571.   testData = "mozilla.com/firefox/its-a-trap.html";
  572.   testUpdate +=
  573.     "n:1000\ni:test-phish-simple\nad:1\n" +
  574.     "a:1:32:" + testData.length + "\n" +
  575.     testData;
  576.  
  577.   var dbService_ = Cc["@mozilla.org/url-classifier/dbservice;1"]
  578.                    .getService(Ci.nsIUrlClassifierDBService);
  579.  
  580.   var listener = {
  581.     QueryInterface: function(iid)
  582.     {
  583.       if (iid.equals(Ci.nsISupports) ||
  584.           iid.equals(Ci.nsIUrlClassifierUpdateObserver))
  585.         return this;
  586.       throw Cr.NS_ERROR_NO_INTERFACE;
  587.     },
  588.  
  589.     updateUrlRequested: function(url) { },
  590.     streamFinished: function(status) { },
  591.     updateError: function(errorCode) { },
  592.     updateSuccess: function(requestedTimeout) { }
  593.   };
  594.  
  595.   try {
  596.     dbService_.beginUpdate(listener,
  597.                            "test-malware-simple,test-phish-simple", "");
  598.     dbService_.beginStream("", "");
  599.     dbService_.updateStream(testUpdate);
  600.     dbService_.finishStream();
  601.     dbService_.finishUpdate();
  602.   } catch(ex) {
  603.     // beginUpdate will throw harmlessly if there's an existing update
  604.     // in progress, ignore failures.
  605.   }
  606.   G_Debug(this, "malwareWarden initialized");
  607. }
  608.  
  609. PROT_MalwareWarden.inherits(PROT_ListWarden);
  610.  
  611. /**
  612.  * Cleanup on shutdown.
  613.  */
  614. PROT_MalwareWarden.prototype.shutdown = function() {
  615.   this.prefs_.removeAllObservers();
  616.  
  617.   this.listManager_ = null;
  618. }
  619.  
  620. /**
  621.  * When a preference changes, we might have to start or stop asking for
  622.  * updates.
  623.  */
  624. PROT_MalwareWarden.prototype.maybeToggleUpdateChecking = function() {
  625.   var malwareWardenEnabled = this.prefs_.getPref(kMalwareWardenEnabledPref,
  626.                                                  null);
  627.  
  628.   G_Debug(this, "Maybe toggling update checking. " +
  629.           "Warden enabled? " + malwareWardenEnabled);
  630.  
  631.   // Do nothing unless thre pref is set
  632.   if (malwareWardenEnabled === null)
  633.     return;
  634.  
  635.   // We update and save to disk all tables
  636.   if (malwareWardenEnabled === true) {
  637.     this.enableBlacklistTableUpdates();
  638.   } else {
  639.     // Anti-malware is off, disable table updates
  640.     this.disableBlacklistTableUpdates();
  641.   }
  642. }
  643.  
  644. /**
  645.  * Deal with a user changing the pref that says whether we should 
  646.  * enable the malware warden.
  647.  *
  648.  * @param prefName Name of the pref holding the value indicating whether
  649.  *                 we should enable the malware warden
  650.  */
  651. PROT_MalwareWarden.prototype.onMalwareWardenEnabledPrefChanged = function(
  652.                                                                     prefName) {
  653.   // Just to be safe, ignore changes to sub prefs.
  654.   if (prefName != kMalwareWardenEnabledPref)
  655.     return;
  656.  
  657.   this.malwareWardenEnabled_ =
  658.     this.prefs_.getPref(prefName, this.malwareWardenEnabled_);
  659.   this.maybeToggleUpdateChecking();
  660. }
  661. //@line 18 "e:\fx19rel\WINNT_5.2_Depend\mozilla\browser\components\safebrowsing\src\nsSafebrowsingApplication.js"
  662.  
  663. var modScope = this;
  664. function Init() {
  665.   var jslib = Cc["@mozilla.org/url-classifier/jslib;1"]
  666.               .getService().wrappedJSObject;
  667.   modScope.G_Debug = jslib.G_Debug;
  668.   modScope.G_Assert = jslib.G_Assert;
  669.   modScope.G_Alarm = jslib.G_Alarm;
  670.   modScope.G_ConditionalAlarm = jslib.G_ConditionalAlarm;
  671.   modScope.G_ObserverWrapper = jslib.G_ObserverWrapper;
  672.   modScope.G_Preferences = jslib.G_Preferences;
  673.   modScope.PROT_XMLFetcher = jslib.PROT_XMLFetcher;
  674.   modScope.BindToObject = jslib.BindToObject;
  675.   modScope.G_Protocol4Parser = jslib.G_Protocol4Parser;
  676.   modScope.PROT_UrlCrypto = jslib.PROT_UrlCrypto;
  677.   modScope.RequestBackoff = jslib.RequestBackoff;
  678.   
  679.   // We only need to call Init once
  680.   modScope.Init = function() {};
  681. }
  682.  
  683. // Module object
  684. function SafebrowsingApplicationMod() {
  685.   this.firstTime = true;
  686.   this.cid = Components.ID("{c64d0bcb-8270-4ca7-a0b3-3380c8ffecb5}");
  687.   this.progid = "@mozilla.org/safebrowsing/application;1";
  688. }
  689.  
  690. SafebrowsingApplicationMod.prototype.registerSelf = function(compMgr, fileSpec, loc, type) {
  691.   if (this.firstTime) {
  692.     this.firstTime = false;
  693.     throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  694.   }
  695.   compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  696.   compMgr.registerFactoryLocation(this.cid,
  697.                                   "Safebrowsing Application Module",
  698.                                   this.progid,
  699.                                   fileSpec,
  700.                                   loc,
  701.                                   type);
  702.   
  703.   compMgr.registerFactoryLocation(this.cid,
  704.                                   "UrlClassifier Blocked Error Page",
  705.                                   "@mozilla.org/network/protocol/about;1?what=blocked",
  706.                                   fileSpec,
  707.                                   loc,
  708.                                   type);
  709. };
  710.  
  711. SafebrowsingApplicationMod.prototype.getClassObject = function(compMgr, cid, iid) {  
  712.   if (!cid.equals(this.cid))
  713.     throw Components.results.NS_ERROR_NO_INTERFACE;
  714.   if (!iid.equals(Ci.nsIFactory))
  715.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  716.  
  717.   return this.factory;
  718. }
  719.  
  720. SafebrowsingApplicationMod.prototype.canUnload = function(compMgr) {
  721.   return true;
  722. }
  723.  
  724. SafebrowsingApplicationMod.prototype.factory = {
  725.   createInstance: function(outer, iid) {
  726.     if (outer != null)
  727.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  728.     Init();
  729.     return new PROT_Application();
  730.   }
  731. };
  732.  
  733. var ApplicationModInst = new SafebrowsingApplicationMod();
  734.  
  735. function NSGetModule(compMgr, fileSpec) {
  736.   return ApplicationModInst;
  737. }
  738.